package com.ccx.service.impl;

import com.ccx.mapper.EbProductCategoryMapper;
import com.ccx.model.EbProductCategory;
import com.ccx.service.EbProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EbProductCategoryServiceImpl implements EbProductCategoryService {
    @Autowired
    private EbProductCategoryMapper ebProductCategoryMapper;

    @Override
    public Map<String, List<EbProductCategory>> findBigAndSmallCategory() {
        //1.查询所有大类
        List<EbProductCategory> bigs = ebProductCategoryMapper.selectBigCategory();
        //System.out.println("所有大类："+bigs);
        //2.查询所有小类
        List<EbProductCategory> smalls = ebProductCategoryMapper.selectSmallCategory();
        //System.out.println("所有小类："+smalls);
        //3.封装到map集合
        Map<String,List<EbProductCategory>> categoryMap= new HashMap<>();
        categoryMap.put("bigs",bigs);
        categoryMap.put("smalls",smalls);

        return categoryMap;
    }


    //根据Id查询大类或小类信息
    @Override
    public EbProductCategory findByBigAndSmallCategory(int epcId) {
        List<EbProductCategory> smalls = ebProductCategoryMapper.selectSmallCategory();
        EbProductCategory eb1 = new EbProductCategory();
        for (EbProductCategory small : smalls) {
            if(small.getEpcId().equals(epcId)){
                return small;
            }
        }

        List<EbProductCategory> bigs = ebProductCategoryMapper.selectBigCategory();
        for (EbProductCategory big : bigs) {
            if(big.getEpcId().equals(epcId)){
                return big;
            }
        }
        return null;
    }

    //查询所有大类信息
    @Override
    public List<EbProductCategory> findBig() {
        return ebProductCategoryMapper.selectBigCategory();
    }

    @Override
    public int update(EbProductCategory ebProductCategory) {
        return ebProductCategoryMapper.updateByPrimaryKeySelective(ebProductCategory);
    }


    public int insert(EbProductCategory ebProductCategory) {
        return ebProductCategoryMapper.insertSelective(ebProductCategory);
    }

    //根据Id查询判断大类或小类进行删除
    @Override
    public int deletefindByBig(int epcId) {
        List<EbProductCategory> bigs = ebProductCategoryMapper.selectBigCategory();
        for (EbProductCategory big : bigs) {
            if (big.getEpcId().equals(epcId)) {
                List<EbProductCategory> smalls = ebProductCategoryMapper.selectSmallCategory();
                for (EbProductCategory samll : smalls) {
                    if (samll.getEpcParentId().equals(epcId)) {
                        return ebProductCategoryMapper.deleteByPrimaryKey(epcId);
                    }
                }
            }
        }
        return ebProductCategoryMapper.deleteByPrimaryKey(epcId);
    }
}
